import pygame,sys,random
from pygame.locals import*


class ball(pygame.sprite.Sprite):
    def _init_(self,image_file,location,speed,lives=3):
        lives=3
        pygame.sprite.Sprite._init_(self)
        self.image=pygame.image.load(image_file)
        self.rect=self.image.get_rect()
        self.rect.topleft=location
        self.speed=speed
        self.lives=lives#                              初始生命
        self.score_point=0
    def _show_lives(self,scrobj):
        if self.lives>0:
            for i in range(self.lives):
                scrobj.blit(self.image,(scrobj.get_width()-40*i,20))
    def _show_score(self,scrobj):
        score_font=pygme.font.Font(Nont,50)
        score_text=score_font.render(str(self.score_point),true,(0,255,255))
        scrobj.blit(score_text,(10,10))
    
    def move_ctrl(self,scrobj,padojb):
        self._show_lives(scrobj)
        self._show_score(scrobj)
        self.rect.move_ip(self.speed)
        scrobj.blit(self,image,self.rect)
        if self.rect.left<0 or self.rect.right>scrobj.get_width():
            self.speed[0]=-self.speed[0]
        if self.rect.top<=0:
            self.score_point +=1
            self.rect.top=0
            self.speed[1]=random.randint(5,10)
        if pygame.sprite.collide_rect(self,padobj):
            self.rect.bottom=padobj.rect.top
            self.speed[1]=-self.speed[1]
        if self.rect.top>=scrobj.get_height():
            self.lives-=1
            self.rect.topleft=(50,50)
            pygame.time.delay(1000)

def is_paddle(x):
    if x<=0 or x>=640:
        return True
    return False

class Paddle(pygame.sprite.Sprite):
    def _init_(self,location):
        pygame.sprite.Sprite._init_(self)
        image_surface=pygame.surface.Surface([100,10])
        image_surface.fill([0,0,0])
        self.image=image_surface.convert()
        self.rect=self.image.get_rect()
        self.rect.topleft=location



    def move_ctrl(self,scrobj):
        if event.type == pygame.KEYDOWN:
            x_old=x
            if event.key == pygame.K_LEFT:
                print("向左")
                if not is_paddle(x-100):                            
                    x-=100
            elif event.key == pygame.K_RIGHT:
                print("向右")
                if not is_paddle (x+100):                            
                    x+=100
            pygame.draw.self.image =(screen, [255,255,255], [x_old,400], c_r)
            scrobj.blit(self.image,self.rect)


pygame.init()
screenn=pygame.display.set_mode([640,480])
clock=pygame.time.Clock()
initspeed=[random.randint(-10,-5),random.randint(5,10)]
mypaddle=Paddle((270,400))
myball=Ball("pingpang.png",(50,50),initspeed,3)




while True:
    if event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()

    clock.tick(30)
    screenn.fill([255,255,255])

    if myball.alive ==3 or myball.alive==2 or myball.alive==1:
        myball.move_ctrl(screen,mypaddle)
        mypaddle.move_ctrl(screen)
    else:
        ft1_font=pygame.font.Font(None,70)
        ft1_surf=ft1_font.render("Game over!",True,[255,0,255])
        ft1_rect=ft1_surf.get_rect()
        ft1_rect.center=(screenn.get_rect().centerx,100)
        ft2_font=pygame.font.Font(None,50)
        ft2_surf=ft2_font.render("your score:",True,[0,255,255])
        ft2_rect=ft2_surf.get_rect()
        ft2_rect.center=(screenn.get_rect().centerx,200)
        screenn.blit(ft1_surf,ft1_rect)
        screenn.blit(ft2_surf,ft2_rect)
        pygame.time.delay(1000)
    pygame.display.flip()
    pygame.quit()